import time
import requests
import json
import random
from lxml import etree
class Content_url(object):
    def __init__(self,post_url) -> None:
        self.post_url = post_url
        self.user_agents = ['Mozilla/5.0 (Windows NT 6.1; rv,2.0.1) Gecko/20100101 Firefox/4.0.1',
                    'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50',
                    'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.84 Safari/537.36',
                    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36',
                    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36 OPR/26.0.1656.60',
                    'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0',
                    'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.133 Safari/534.16',
                    'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko',
                    'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.71 Safari/537.1 LBBROWSER'
                    ]  #请求头pool
    def get_content_url(self,page,data_dict,data_list):
        headers = {
            'User-Agent': random.choice(self.user_agents),   
            'Content-Type': 'application/json;charset=UTF-8'
        }
        param =  (
            ('rand', '0.7149689263916039'),
            ('page', page),
            ('size', '100'),
        )
        payload = '{}'
        response = requests.post(url=self.post_url,headers=headers,params=param,data=payload).text
        json_data = json.loads(response)    
        content_list = json_data['content']
        for content in content_list:
            data_dict = {}     #每次往列表添加字典都要创建一个空字典，避免key同名覆盖
            section = '私募基金管理人分类查询公示'
            headline = content['managerName']
            
            timeStamp = content['registerDate']
            timeArray = time.localtime(timeStamp/1000)     #time.localtime(timeStamp/1000) 只能处理10位数的时间戳
            check_in_time = time.strftime("%Y-%m-%d", timeArray)
            
            content_url = 'https://gs.amac.org.cn/amac-infodisc/res/pof/manager/' + str(content['url'])     #得到详情页链接     
            data_dict['section'] = section
            data_dict['headline'] = headline
            data_dict['time'] = check_in_time
            data_dict['url'] = content_url
            data_list.append(data_dict)
        return data_list
            
            

    